using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using Entity;
using Utilities;


namespace DAL
{
    public class OutBillDataAccess : FreshCmsAccessBase
    {
        #region code CMS
        public List<OutBill> GetOutBillList()
        {
            try
            {
                const string spName = "spo_OutBill_GetList";

                var OutBillList = DbHelper.ExecuteSPList<OutBill>(spName);
                return OutBillList;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return new List<OutBill>();
            }
        }

        public List<OutBill> GetOutBillListByPaging(int pageIndex, int pageSize, ref int totalRecord)
        {
            try
            {
                const string spName = "spo_OutBill_GetByPaging";
                var sqlParams = new[]
                                    {
										new SqlParameter("@PageIndex", pageIndex),
										new SqlParameter("@PageSize", pageSize),
										new SqlParameter("@TotalRecord", totalRecord)
                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@TotalRecord"].Direction = ParameterDirection.Output;
                var OutBillList = DbHelper.ExecuteSqlList<OutBill>(sqlCommand);
				if (sqlCommand.Parameters["@TotalRecord"].Value != null)
                {
                    totalRecord = (int) sqlCommand.Parameters["@TotalRecord"].Value;
                }

                return OutBillList;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return new List<OutBill>();
            }
        }

        public OutBill GetOutBillById(int billId)
        {
            try
            {
                const string spName = "spo_OutBill_GetById";
                var sqlParams = new[]
                                    {
										new SqlParameter("@BillId", billId)
                                    };

                var OutBill = DbHelper.ExecuteSPList<OutBill>(spName, sqlParams).SingleOrDefault();
                return OutBill;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return null;
            }
        }

        public int InsertOutBill(OutBill OutBill)
        {
            try
            {
                const string spName = "spo_OutBill_Insert";
                var sqlParams = new[]
                                    {

										new SqlParameter("@BillName", OutBill.BillName),
										new SqlParameter("@BillEmail", OutBill.BillEmail),
										new SqlParameter("@BillAddress", OutBill.BillAddress),
										new SqlParameter("@BillPhone", OutBill.BillPhone),
										new SqlParameter("@BillDesc", OutBill.BillDesc),
										new SqlParameter("@BillDate", OutBill.BillDate),
										new SqlParameter("@BillTotal", OutBill.BillTotal),
										new SqlParameter("@BillStatus", OutBill.BillStatus),
										new SqlParameter("@CusId", OutBill.CusId),
										new SqlParameter("@BillId", OutBill.BillId),

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@BillId"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@BillId"].Value != null)
                {
                    return (int) sqlCommand.Parameters["@BillId"].Value;
                }

				return 0;


            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return -1;
            }
        }

        public int UpdateOutBill(OutBill OutBill)
        {
            try
            {
                const string spName = "spo_OutBill_Update";
                var sqlParams = new[]
                                    {
										new SqlParameter("@BillId", OutBill.BillId)
										, new SqlParameter("@BillName", OutBill.BillName)
										, new SqlParameter("@BillEmail", OutBill.BillEmail)
										, new SqlParameter("@BillAddress", OutBill.BillAddress)
										, new SqlParameter("@BillPhone", OutBill.BillPhone)
										, new SqlParameter("@BillDesc", OutBill.BillDesc)
										, new SqlParameter("@BillDate", OutBill.BillDate)
										, new SqlParameter("@BillTotal", OutBill.BillTotal)
										, new SqlParameter("@BillStatus", OutBill.BillStatus)
										, new SqlParameter("@CusId", OutBill.CusId)
                                    };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return -1;
            }
        }

        public int DeleteOutBill(int billId)
        {
            try
            {
                const string spName = "spo_OutBill_Delete";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@BillId", billId)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return -1;
            }
        }
		#endregion
    }
}
